;
define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require("base"); var supplierBase = require("supplierBase");
    var roleId = base.getUrlParam("roleId");
    var sidebar = require("supplier/systemSidebar/systemSidebar");
    var hashJump = require("supplier/hash/hashJump");
    var role_array = [{
		id: "mainPage_1",
		name: "首页",
		sub:[{
			id: "financingApprove_1",
			name: "融资审批"
		}, {
			id: "contractUnsign_1",
			name: "合同上传"
		}, {
			id: "noticeReceive_1",
			name: "通知装货"
		}, {
			id: "financeUnsupervise_1",
			name: "确认监管"
		}, {
			id: "financeUnrelease_1",
			name: "放款确认"
		}, {
			id: "financeUntransport_1",
			name: "通知运输"
		}, {
			id: "financePickupExam_1",
			name: "提货审批"
		}, {
			id: "financeUnreceive_1",
			name: "收款确认"
		}, {
			id: "financeUnreceiveBulkstock_1",
			name: "放货通知"
		}, {
			id: "cancelSupervise_1",
			name: "监管解除"
		}, {
			id: "pickupPre_1",
			name: "提前还款"
		}, {
			id: "pickupDelay_1",
			name: "延期还款"
		}, {
			id: "interestDelay_1",
			name: "延期计息"
		}, {
			id: "detained_1",
			name: "扣货申请"
		}, {
			id: "pickupRansom_1",
			name: "赎货申请"
		}, {
			id: "badassetsHandle_1",
			name: "不良资产"
		}, {
			id: "abnormalManage_1",
			name: "异常管理"
		}]
	}, {
		id: "financingManage_1",
		name: "融资项目：",
		sub: [{
			id: "financeApplicationManage_1",
			name: "融资申请"
		}, {
			id: "prepaymentProcessing_1",
			name: "提前还款处理"
		}, {
			id: "delayPaymentProcessing_1",
			name: "延期还款处理"
		}, {
			id: "deferredPaymentProcessing_1",
			name: "扣货申请处理"
		}, {
			id: "redeemApplicationProcessing_1",
			name: "赎货申请处理"
		}, {
			id: "badAssetsProcessing_1",
			name: "不良资产处理"
		}]
	}, {
		id: "clientManage_1",
		name: "客户管理：",
		sub: [{
			id: "enterpriseUserManage_1",
			name: "企业用户管理"
		}, {
			id: "logisticsUserManage_1",
			name: "物流公司管理"
		}]
	}, {
		id: "bulkstockManage_1",
		name: "大宗商品：",
		sub: [{
			id: "bulkstockCategoryManage_1",
			name: "商品类别管理"
		}, {
			id: "bulkstockPriceCollection_1",
			name: "商品价格采集"
		}, {
			id: "priceChecked_1",
			name: "汇总价格已审核发布"
		}, {
			id: "priceUnchencked_1",
			name: "汇总价格已待审核"
		}]
	}, {
		id: "newsManage_1",
		name: "新闻资讯：",
		sub: [{
			id: "newsInformationManage_1",
			name: "新闻资讯管理"
		}, {
			id: "newsBannerManage_1",
			name: "栏目管理"
		}]
	}, {
		id: "exceptionManage_1",
		name: "异常管理",
		sub: [{
			id: "exceptionUnprocess_1",
			name: "未处理异常"
		},{
			id: "exceptionAccepted_1",
			name: "已接受异常"
		},{
			id: "exceptionProcessing_1",
			name: "调查中异常"
		},{
			id: "exceptionCompleted_1",
			name: "处理完毕异常"
		}]
	},{
		id: "systemManage_1",
		name: "系统管理：",
		sub: [{
			id: "orgaizationManage_1",
			name: "组织管理"
		}, {
			id: "permisionGroupManage_1",
			name: "角色权限"
		}, {
			id: "announceManage_1",
			name: "公告管理"
		}, {
			id: "advertisementManage_1",
			name: "广告管理"
		}, {
			id: "memberGradeManage_1",
			name: "会员等级管理"
		}, {
			id: "systemParamManage_1",
			name: "系统参数管理"
		}, {
			id: "contractTemplateManage_1",
			name: "合同范本管理"
		},{
			id: "systemParamManage_2",
			name: "费用类型"
		}]
	}];
    var init = function() {
        initEvent();
        sidebar.init();
        permissionsListHandler(role_array);
        hashJump.init();
    };
    var permissionsListHandler = function(data) {
        var hs = [];
        for (var i = 0; i < data.length; i++) {
            hs.push('<tr class="tr_add">');
            hs.push('<td valign="top" width="14%" class="cell left_cell">');
            hs.push('<span class="check_div">');
            hs.push('<input id="checkAll_' + i + '" class="checkAll_common"  group="' + i + '" name="weiguidang" type="checkbox" value=""/>');
            hs.push('<label for="checkAll_' + i + '" group=group"' + i + '" class="feedCkLabel">' + data[i].name + '</label>');
            hs.push('</span></td>');
            hs.push('<td class="cell right_cell" width="86%">');
            hs.push('<div id="checks_' + i + '">');
            for (var j = 0; j < data[i].sub.length; j++) {
                hs.push('<span class="check_div">');
                hs.push('<input id="listitem_' + i + j + '" name="group' + i + '" name="permissionId" type="checkbox" value="' + data[i].sub[j].id + '"/>');
                hs.push('<label for="listitem_' + i + j + '"   name="group' + i + '" name="permissionId" class="feedCkLabel" permissionId="' + data[i].sub[j].id + '">' + data[i].sub[j].name + '</label>');
                hs.push('</span>');
            };
            hs.push('</div>');
            hs.push('</td>');
        };
        hs.push('</tr>');
        $("#permisiongroup_add_table").html(hs.join(""));
        loadExists();
        
    };
    var initEvent = function() {
    	//修改顶部栏的选中状态
		$(".nav_hover",window.parent.document).removeClass("head_link_1");
		$("#airportCodeMainten",window.parent.document).addClass("head_link_1");
		
        reverseChoice();
        //设置初始权限名
        // 选择增加人员
        $("#permisiongroup_add_table").on("click", ".feedCkLabel", function() {
            $(this).toggleClass("feedCkChecked");
        });
        $("#office_checkAll").on("click", function() {
            var cks = $("[name=office]:checkbox");
            if ($(this).is(":checked")) {
                cks.each(function(i) {
                    $(cks[i]).prop("checked", true);
                    $(cks[i]).siblings(".feedCkLabel").addClass("feedCkChecked");
                });
            } else {
                cks.each(function(i) {
                    $(cks[i]).prop("checked", false);
                    $(cks[i]).siblings(".feedCkLabel").removeClass("feedCkChecked");
                });
            }
        });
        $("#permisiongroup_add_table").on("click", ".checkAll_common", function() {
            var group = $(this).attr("group");
            var con = $("#checks_" + group);
            var cks = con.find("[name=group" + group + "]:checkbox");
            if ($(this).is(":checked")) {
                cks.each(function(i) {
                    $(cks[i]).prop("checked", true);
                    $(cks[i]).siblings(".feedCkLabel").addClass("feedCkChecked");
                });
            } else {
                cks.each(function(i) {
                    $(cks[i]).prop("checked", false);
                    $(cks[i]).siblings(".feedCkLabel").removeClass("feedCkChecked");
                });
            }
        });
        $("#but_confirm").click(function() {
            roleEdit();
        });
    };
    //反选选中框
    var reverseChoice = function() {
        $("#permisiongroup_add_table").on("click", "tr.tr_add", function() {
            var $this = $(this);
            var cellLabelLeng = $this.find(".right_cell .feedCkLabel ").length;
            var cellChecked = $this.find(".right_cell .feedCkChecked").length;
            if (cellChecked == cellLabelLeng) { 
                $this.find(".left_cell .feedCkLabel").addClass("feedCkChecked");
            } else { 
                $this.find(".left_cell .feedCkLabel").removeClass("feedCkChecked");
            }
        });
    };
    //修改权限接口
    var roleEdit = function() {
        var body = {};
        body.permissionId = [];
        $(".check_div .feedCkLabel.feedCkChecked").each(function(index, obj) {
            if (!$(obj).attr('permissionId') || $(obj).attr('permissionId').length == 0) {
            }else{
                body.permissionId.push($(obj).attr('permissionId'));
            };
        });
        var role_name = $("#permisiongroup_add_input").val();
        if (role_name.length == 0) {
             util.alert("权限组名称输入不能为空");
                return false;
        };
        if (role_name.length>255) {
             util.alert("权限组名称长度不能超过255个字符");
                return false;
        };
        body.roleName = role_name;
        supplierBase.fnAjax({
            action: "updateRole.json",
            oData: {
                permissionId: body.permissionId,
                roleId: roleId,
                roleName: body.roleName
            },
            fnSuccess: function(dt) {
                if (dt.status == 200) {
                    $("#but_confirm").unbind();
                    // base.openUrl("../permisionManage/permisionManage.html");
                    hashJump.hashJump("permisionManage", false, "");
                };
            }
        });
    };
    //使权限组已有的权限组getChecked
    var loadExists = function() {
        supplierBase.fnAjax({
            action: "inquireRoleDetail.json",
            oData: {
                roleId: roleId
            },
            fnSuccess: function(data) {
                var cs = $("#permisiongroup_add_table .feedCkLabel");
                 $("#permisiongroup_add_input").val(data.datas[0].roleName);
                $.each(cs, function(idx, itm) {
                    var _t = $(itm);
                    $.each(data.datas[0].perm, function(ii, tt) {
                        if (tt.permissionId == _t.attr("permissionId")) {
                            _t.addClass("feedCkChecked");
                        }
                    });

                });
                $("tr.tr_add").trigger("click");
            }
        });
    };
    return {
        init: init
    };
});